﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;

namespace HRM.UI.HRManagerUI.EmpModule
{
    public partial class EditEmployeeContacts : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                searchEditcontacdiv.Visible = false;

            }

        }

        protected void SearchEditContact(object sender, EventArgs e)
        {
            try
            {
                Code code = new Code();
                code.CodeName = txtEmployeeCode.Value;
                ContactManager manager = new ContactManager();
                Contact contact = new Contact();
                Employee employee = new Employee();
                employee.EmpCode = code;
                contact.Employee = employee;
                contact = manager.GetEmployeeContactInfo(contact);
                if (contact.FirstContact != null)
                {
                    searchEditcontacdiv.Visible = true;
                    txtEmpCode.Value = code.CodeName;
                    txtFirstcontact.Value = contact.FirstContact;
                    txtSecondContact.Value = contact.Secondcontact;
                    txtEmergencyperson.Value = contact.EmergencyContactPerson;
                    txtEmergencyNumber.Value = contact.EmergencyContactNumber;
                    txtPresentAddress.Value = contact.PresentAddress;
                    txtPermanentAddress.Value = contact.PermanentAddress;
                    msgLabel.Text = "";

                }
                else
                {
                    searchEditcontacdiv.Visible = false;
                    msgLabel.Text = "Data Not Found for Employe " + code.CodeName;
                    msgLabel.ForeColor = Color.Red;
                }


            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void UpdateEditContact(object sender, EventArgs e)
        {
            try
            {
                ContactManager manager = new ContactManager();
                Contact contact = new Contact();
                Code code = new Code();
                code.CodeName = txtEmpCode.Value;
                Employee employee = new Employee();
                employee.EmpCode = code;
                contact.Employee = employee;
                contact.FirstContact = txtFirstcontact.Value;
                contact.Secondcontact = txtSecondContact.Value;
                contact.EmergencyContactPerson = txtEmergencyperson.Value;
                contact.EmergencyContactNumber = txtEmergencyNumber.Value;
                contact.PresentAddress = txtPresentAddress.Value;
                contact.PermanentAddress = txtPermanentAddress.Value;
                string msg = manager.UpdateContact(contact);
                msgLabel.Text = msg;
                if (msg.Contains("Successfully"))
                {
                    searchEditcontacdiv.Visible = false;
                    msgLabel.ForeColor = Color.Green;
                    msgLabel.Font.Bold = true;
                }
                else
                {
                    msgLabel.ForeColor = Color.Red;
                    msgLabel.Font.Bold = true;
                }

            }
            catch (Exception ex)
            {

                msgLabel.ForeColor = Color.Red;
                msgLabel.Font.Bold = true;
                msgLabel.Text = ex.Message;
            }

        }
        protected void ClearInput()
        {
            txtFirstcontact.Value = "";
            txtEmergencyperson.Value = "";
            txtEmergencyNumber.Value = "";
            txtPresentAddress.Value = "";
            txtPermanentAddress.Value = "";
            txtSecondContact.Value = "";
        }
    }
}